# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Smeltzov <andrew.smeltzov@gmail.com>

_realname=pyside2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt5)
pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (mingw-w64)"
pkgver=5.15.2
pkgrel=7
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://doc.qt.io/qtforpython-5/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-shiboken2-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-declarative")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-winextras"
            "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
            "${MINGW_PACKAGE_PREFIX}-qt5-tools"
            "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
            "${MINGW_PACKAGE_PREFIX}-qt5-location"
            "${MINGW_PACKAGE_PREFIX}-qt5-quickcontrols2"
            "${MINGW_PACKAGE_PREFIX}-qt5-remoteobjects"
            "${MINGW_PACKAGE_PREFIX}-qt5-scxml"
            "${MINGW_PACKAGE_PREFIX}-qt5-script"
            "${MINGW_PACKAGE_PREFIX}-qt5-sensors"
            "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
            "${MINGW_PACKAGE_PREFIX}-qt5-speech"
            "${MINGW_PACKAGE_PREFIX}-qt5-charts"
            "${MINGW_PACKAGE_PREFIX}-qt5-svg"
            "${MINGW_PACKAGE_PREFIX}-qt5-datavis3d"
            "${MINGW_PACKAGE_PREFIX}-qt5-activeqt"
            "${MINGW_PACKAGE_PREFIX}-qt5-webchannel"
            "${MINGW_PACKAGE_PREFIX}-qt5-websockets"
            "${MINGW_PACKAGE_PREFIX}-qt5-3d")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-qt5-winextras"
             "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
             "${MINGW_PACKAGE_PREFIX}-qt5-location"
             "${MINGW_PACKAGE_PREFIX}-qt5-quickcontrols2"
             "${MINGW_PACKAGE_PREFIX}-qt5-remoteobjects"
             "${MINGW_PACKAGE_PREFIX}-qt5-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt5-script"
             "${MINGW_PACKAGE_PREFIX}-qt5-sensors"
             "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
             "${MINGW_PACKAGE_PREFIX}-qt5-speech"
             "${MINGW_PACKAGE_PREFIX}-qt5-charts"
             "${MINGW_PACKAGE_PREFIX}-qt5-svg"
             "${MINGW_PACKAGE_PREFIX}-qt5-datavis3d"
             "${MINGW_PACKAGE_PREFIX}-qt5-activeqt"
             "${MINGW_PACKAGE_PREFIX}-qt5-webchannel"
             "${MINGW_PACKAGE_PREFIX}-qt5-websockets"
             "${MINGW_PACKAGE_PREFIX}-qt5-3d"
             "${MINGW_PACKAGE_PREFIX}-qt5-gamepad")
options=('staticlibs' 'strip')
_pkgfqn=pyside-setup-opensource-src-$pkgver
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/${_pkgfqn}.tar.xz
        001-pyside.patch)
sha256sums=('b306504b0b8037079a8eab772ee774b9e877a2d84bab2dbefbe4fa6f83941418'
            'a941b8e289b9825be086b83c326ded18b64ac78c094e013fd1e7fa56d976b3c1')

prepare() {
  cd "${srcdir}"/$_pkgfqn
  patch -p1 -i ${srcdir}/001-pyside.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_TESTS=OFF \
    ${_conf} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ../${_pkgfqn}/sources/pyside2

  MSYS2_ARG_CONV_EXCL="--include-paths=;--typesystem-paths=" \
  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake.exe --install .

  local _PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/PySide2-${pkgver}/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  # Install egg-info
  cd "${srcdir}"/$_pkgfqn
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=pyside2
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r pyside2.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}
